package com.leetcode.p0;

import com.bean.ListNode;
import com.utils.DataUtils;

public class Test61 {

    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || k == 0) {
            return head;
        }

        ListNode cur = head;
        ListNode tail = head;

        int n = 0;
        while (cur != null) {
            tail = cur;
            cur = cur.next;
            n++;
        }

        k %= n;

        ListNode p = head;
        for (int i = 0; i < n - k - 1; i++) {
            p = p.next;
        }

        tail.next = head;
        head = p.next;
        p.next = null;

        System.out.println(n);

        return head;
    }

    public static void main(String[] args) {
        ListNode listNode = DataUtils.getOneToNumberListNode(3);

        Test61 test = new Test61();
        ListNode list = test.rotateRight(listNode, 4);
        DataUtils.printListNode(list);
    }

}
